<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button size="samll" type="primary" @click="btnOK">确定</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailsById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [],
      checkList: [],
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    // 获取员工信息
    async getCheckedRole(id) {
      const { roleIds } = await getUserDetailsById(id)
      this.userId = id
      this.checkList = roleIds
    },
    // 关闭弹窗
    btnCancel() {
      this.$emit('update:showDialog', false)
    },
    // 确定按钮
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.checkList })
      // 提示用户
      this.$message.success('分配成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
